<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container" style="text-align:center">
       <div>核销</div>
       <div style="margin-top:15px">
           <el-input v-model="listQuery.orderGoodsId" clearable class="filter-item" style="width: 200px;" placeholder="请输入卷码" />
           <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
       </div>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="商品图片" prop="picUrl">
        <template slot-scope="scope">
            <img :src="scope.row.picUrl" width="40" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="商品名称" prop="goodsName" />

      <el-table-column align="center" label="金额" prop="price" />

      <el-table-column align="center" label="购买数量" prop="number" />

      <el-table-column align="center" label="核销数量" prop="writeOffNum">
        <template slot-scope="">
            <input type="number" v-model="num" oninput="if(value > 5000004 || value < 1 ){alert('非法输入！')}"/>
        </template>
      </el-table-column>

      <el-table-column align="center" label="核销时间" prop="writeOffTime" />

      <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)" v-if="scope.row.writeOffStatus != 1 ">核销</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />


  </div>
</template>
<style scoped>
 input{
     /* border: none; */
     width: 45px;
 }
</style>
<script>
import { findById, wxWriteOff } from '@/api/comment'
import { replyComment } from '@/api/order'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'writedet',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      num:'',
      listLoading: true,
      listQuery: {
          orderGoodsId:''
      },
      downloadLoading: false,
      replyForm: {
        commentId: 0,
        content: ''
      },
      replyFormVisible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      findById(this.listQuery).then(response => {
        var lis = []
        lis.push(response.data.data)
        console.log(lis)
        this.list = lis
        this.total = response.data.data.total
        this.listLoading = false
        this.listQuery.orderGoodsId = ''
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.getList()
    },
    handleReply(row) {
      this.replyForm = { commentId: row.id, content: '' }
      this.replyFormVisible = true
    },
    reply() {
      replyComment(this.replyForm).then(response => {
        this.replyFormVisible = false
        this.$notify.success({
          title: '成功',
          message: '核销成功'
        })
      }).catch(response => {
        this.$notify.error({
          title: '失败',
          message: response.data.errmsg
        })
      })
    },
    handleDelete(row) {
        this.$confirm('确认核销', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           if(this.num > row.number){
                this.$notify.error({
                    title: '提示',
                    message: "核销数大于购买数量，请重新输入"
                })
     }else{
        wxWriteOff({orderGoodsId:row.id,writeOffNum:this.num}).then(response => {
            if(response.data.errno == 0){
                this.$notify.success({
                    title: '成功',
                    message: '回复成功'
                })
                var lis = []
                lis.push(response.data.data)
                this.list = lis
                this.num = 0
            }else{
                this.$notify.error({
                    title: '失败',
                    message: response.data.errmsg
                })
            }
        })
     }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
     
      
    },
  }
}
</script>
